/*
 * 
 */
package com.android.sq.zouqi.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.pushservice.PushService;
import com.android.sq.zouqi.util.ServerData;

public class pCtrlUserInfo {
	private final static String TAG = "pCtrlUserInfo";
	private Handler handler;
	private int user_id; 
	private String phone_number;
	private SharedPreferences share;
	private Context context;
	private ServerData server_data;
	public boolean isFinishLogin = false;
	private String url_checkNum = "/zouqi/UserManage/pCheckNumOnServer.php";
	private String url_userRegist = "/zouqi/UserManage/pUserReg.php";
	public pCtrlUserInfo(Context context, Handler handler) {
		this.context = context;
		this.handler = handler;
		share = context.getSharedPreferences(Constants.SP_USER_INFO, Context.MODE_PRIVATE);
	}
	
	// 检查本地是否有用户已登录 - pCC01F01
	// 0为默认值
	public boolean pIfLocalLogin() {
		Log.e("BA", "pCtrlUserInfo -> pIfLocalLogin");
		user_id = share.getInt(Constants.SP_USER_ID, 0);
		if (user_id != 0) {
			Log.e("BA", "有，本地SharedPreferences中有user_id = " + user_id);
			return true;
		} else {
			Log.e("BA", "没有，本地SharedPreferences中没有user_id = " + user_id);
			return false;
		}
	}
	
	// 获得本机号码 - pCC01F02
	public String pGetLocalCellNumber() {
		Log.e("BA", "pCtrlUserInfo -> pGetLocalCellNumber");
		TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		phone_number = telephonyManager.getLine1Number();
		//phone_number = telephonyManager.getDeviceId();
		phone_number = "15555215554";	// 因为M9获取不到手机号码，只能写死测试...
		Log.e("BA", "pCtrlUserInfo -> pGetLocalCellNumber, phone_number = " + phone_number);
		return phone_number;
	}
	
	// 检查服务器端是否存在此号码 - pCC01F03
	// 如果服务器端存在此号码则返回相应的用户id，否则返回null
	/*public int pCheckNumOnServer(String cellphone_num) {
		Log.e("BA", "pCtrlUserInfo -> pCheckNumOnServer");
		//boolean isNumOnServer = false;
		HttpPost httpRequest = new HttpPost(checkNum_url);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_PHONE_NUMBER, cellphone_num));
		try {
			httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			//Log.e("BA", "pCtrlUserInfo -> pCheckNumOnServer, statusCode = " + statusCode);
			if (statusCode == 200) {
				String userid_str = EntityUtils.toString(httpResponse.getEntity());
				user_id = Integer.parseInt(userid_str.trim());
				Log.e("BA", "pCtrlUserInfo -> pCheckNumOnServer, user_id = " + user_id);
				//Log.e("BA", "CHECK NUM，GET userid = " + user_id);
			} else {
				user_id = 0;
				handler.sendEmptyMessage(Constants.CHECK_NUM_ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//Log.e("BA", "pCtrlUserInfo -> pCheckNumOnServer, USER_id = " + user_id);
		return user_id;
	}*/
	// 检查服务器端是否存在此号码 - pCC01F03
		// 如果服务器端存在此号码则返回相应的用户id，否则返回null
	public int pCheckNumOnServer(String cellphone_num) {
		Log.e(TAG, "pCheckNumOnServer");
		user_id = 0;
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_PHONE_NUMBER, cellphone_num));
		
		server_data = new ServerData(Constants.SERVER_URL + url_checkNum, params);
		String result_str = server_data.getResultString();
		Log.e(TAG, "pCheckNumOnServer,result = " + result_str);
		user_id = Integer.parseInt(result_str);
		return user_id;
	}
	
	/*
	 *  用户注册 - pCC01F04
	 *  往数据库中写入一条新记录，如果写入成功则返回该数据的_id，否则返回0。
	 */
	/*public int pUserReg(String phone_number) {
		Log.e("BA", "pCtrlUserInfo -> pUserReg");
		HttpPost httpRequest = new HttpPost(url_userRegist);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_PHONE_NUMBER, phone_number));
		try {
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				String userid_str = EntityUtils.toString(httpResponse.getEntity());
				user_id = Integer.parseInt(userid_str.trim());
				Log.e("BA", "pCtrlUserInfo -> pUserReg, Get USER_id = " + user_id);
				//Log.e("BA", "USER REGIST, GET userid = " + user_id);
			} else {
				user_id = 0;
				handler.sendEmptyMessage(Constants.AUTO_REGIST_ERROR);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		//Log.e("BA", "pCtrlUserInfo -> pUserReg, GET user_id = " + user_id);
		return user_id;
	}*/
	/*
	 *  用户注册 - pCC01F04
	 *  往数据库中写入一条新记录，如果写入成功则返回该数据的_id，否则返回0。
	 */
	public int pUserReg(String phone_number) {
		Log.e("pCtrlUserInfo", "pUserReg!");
		user_id = 0;
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_PHONE_NUMBER, phone_number));
		ServerData server_data = new ServerData(Constants.SERVER_URL + url_userRegist, params);
		String result_str = server_data.getResultString();
		Log.e("pCtrlUserInfo", "result_str = " + result_str);
		user_id = Integer.parseInt(result_str);
		return user_id;
	}
	
	// 用户登录 - pCC01F05
	// 将用户id放入全局变量
	// 新加：推送服务也启动，用user_id当推送服务的client id只建立服务链接，未添加主题
	// 备注：不用返回boolean，因为user_id放入share无须返回
	public void pUserLogin() {
		Log.e("BA", "pCtrlUserInfo -> pUserLogin, user_id = " + user_id);
		share.edit().putInt(Constants.SP_USER_ID, user_id).commit();
		Log.e("BA", "pUserLogin完成, user_id = " + share.getInt(Constants.SP_USER_ID, 0));
		//handler.sendEmptyMessage(Constants.FINISH_LOGIN);
		isFinishLogin = true;
		//Log.e("BA", "is FinishLogin = " + isFinishLogin);
	}
 }
